﻿using System;
using System.Drawing;

namespace zykControl
{
    internal partial class FrmCheckedComboBoxDrop
    {
        public FrmCheckedComboBoxDrop()
        {
            InitializeComponent();
        }

        private CheckedComboBox? mOwner;
        private Point mLocation;

        public void ShowA(CheckedComboBox theOwner)
        {
            mOwner = theOwner;
            if (theOwner.Parent != null) mLocation = theOwner.Parent.PointToScreen(new Point(theOwner.Left, theOwner.Top + theOwner.Height));
            Visible = false;
            Show(theOwner.FindForm());
        }

        // Private Sub FrmCheckedComboBoxDrop_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
        // If mOwner.FindForm.ActiveControl IsNot mOwner Then Visible = False
        // End Sub

        private void 全部选中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int xx = 0, loopTo = CheckedListBox1.Items.Count - 1; xx <= loopTo; xx++)
                CheckedListBox1.SetItemChecked(xx, true);
            CheckedListBox1_SelectedValueChanged(sender, e);
        }

        private void 全部取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int xx = 0, loopTo = CheckedListBox1.Items.Count - 1; xx <= loopTo; xx++)
                CheckedListBox1.SetItemChecked(xx, false);
            CheckedListBox1_SelectedValueChanged(sender, e);
        }

        private void FrmCheckedComboBoxDrop_Load(object sender, EventArgs e)
        {
            Location = mLocation;
            if (mOwner != null) Width = mOwner.Width;
        }

        private void CheckedListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (mOwner is null) return;

            string dKeys = "";
            string dDisployText = "";
            foreach (var dItem in CheckedListBox1.CheckedItems)
            {
                if (dItem is CheckedComboBoxItem item)
                {
                    dKeys += item.Key + ",";
                    dDisployText += item.Description + ",";
                }
                else if (dItem is string || dItem is ValueType)
                {
                    dKeys += dItem.ToString() + ",";
                    dDisployText += dItem.ToString() + ",";
                }
                else if (!string.IsNullOrEmpty(CheckedListBox1.ValueMember) && !string.IsNullOrEmpty(CheckedListBox1.DisplayMember) && dItem.GetType().GetProperty(CheckedListBox1.ValueMember) is not null && dItem.GetType().GetProperty(CheckedListBox1.DisplayMember) is not null)
                {
                    try
                    {
                        dKeys += dItem.GetType().GetProperty(CheckedListBox1.ValueMember)?.GetValue(dItem, null)?.ToString();
                        dDisployText += dItem.GetType().GetProperty(CheckedListBox1.DisplayMember)?.GetValue(dItem, null)?.ToString();
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    dKeys += dItem.ToString() + ",";
                    dDisployText += dItem.ToString() + ",";
                }
            }

            if (dKeys.Length > 0) dKeys = dKeys[..^1];
            mOwner.CloseAsync = true;
            mOwner.Value = dKeys;
            mOwner.Text = dDisployText;
            mOwner.CloseAsync = false;
        }

        private void FrmCheckedComboBoxDrop_Activated(object? sender, EventArgs e)
        {
            Location = mLocation;
            if (mOwner != null) Width = mOwner.Width;
        }
    }

    /// <summary>CheckedComboBox的项,CheckedComboBox是用于DataGridView使用的</summary>
    public class CheckedComboBoxItem
    {
        /// <summary>键</summary>
        public string? Key { get; set; }
        /// <summary>说明</summary>
        public string? Description { get; set; }

        /// <summary>已重写,显示Description的内容</summary>
        public override string ToString()
        {
            return Description ?? "";
        }
    }
}